package com.ruoyi.system.tools;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.ValueFilter;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class FastJsonUtil implements ValueFilter {
	private FastJsonUtil() {
	}

	private static class HolderClass {
		private final static FastJsonUtil instance = new FastJsonUtil();
	}

	public static FastJsonUtil getInstance() {
		return HolderClass.instance;
	}

	@Override
	public Object process(Object object, String name, Object value) {
		if (null == value) {
			return "";
		}
		return value;
	}

	public JSONObject getJSONObject(Map<String, Object> object) {
		return JSONObject.parseObject(JSON.toJSONString(object, this));
	}

	public JSONArray getJSONArray(List<Map<String, Object>> object) {
		return JSONArray.parseArray(JSON.toJSONString(object, this));
	}
}
